// @ts-nocheck
import ininters from '../utlis/server';

function Copylist(arr: any, ele: any) {
  let ss = JSON.parse(JSON.stringify(arr));

  return ss;
}
const IndexModel = {
  namespace: 'index',

  state: {
    shopslist: [],
  },

  effects: {
    // @ts-ignore

    *query({ payload }, { call, put, select }) {
      const list = yield select((s) => {
        return s.index.shopslist;
      });

      let index = list.findIndex((item) => {
        return item._id == payload.ita._id;
      });

      if (index != -1) {
        let arr = Copylist(list, payload.ita);

        arr.forEach((element) => {
          if (element._id == payload.ita._id) {
            element.num = payload.ita.num;
          }
        });

        yield put({ type: 'addnum', payload: { list: arr } });
      } else {
        let arr = Copylist(list, payload.ita);
        arr.push(payload.ita);
        yield put({ type: 'add', payload: { list: arr } });
      }

      // yield put({ type: 'deles', payload: payload });
    },
  },
  reducers: {
    add(state: any, action: any) {
      console.log(action.payload.list);

      state.shopslist = action.payload.list;
    },
    addnum(state: any, action: any) {
      console.log(action.payload.list);

      state.shopslist = [...action.payload.list];
    },
    init(state: any, action: any) {
      state.shopslist = action.payload.ita;
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
};

export default IndexModel;
